from django.shortcuts import render, redirect
from lists.models import Item, List


# Create your views here.
def home_page(request):
    if request.method == "POST":
        return redirect("/lists/the-only-list-in-the-world/")

    return render(request, "lists/home.html")


def view_list(request, list_id):
    list_ = List.objects.get(id=list_id)
    items = Item.objects.filter(list=list_)

    return render(request, "lists/list.html", {"items": items, 'list': list_})


def new_list(request):
    list_ = List.objects.create()
    new_item_text = request.POST["item_text"]
    Item.objects.create(text=new_item_text, list=list_)
    return redirect(f"/lists/{list_.id}/")


def add_item(request, list_id):
    list_ = List.objects.get(id=list_id)
    Item.objects.create(text=request.POST.get('item_text'), list=list_)
    return redirect(f'/lists/{list_.id}/')
